package code.google.com.analyticsbot.data;

import java.io.Serializable;
import java.util.Calendar;
import java.util.logging.Logger;
import code.google.com.analyticsbot.AnalyticsbotServlet;

public class UserData implements Serializable
{
	private static final long serialVersionUID = -2087769926427517724L;
	private static final Logger log = Logger.getLogger(AnalyticsbotServlet.class.getName());

	private String m_id;
	private int m_created = 0;
	private int m_modified = 0;
	private String m_inviter = null;
	private boolean m_isParticipant=true;

	private int[] m_activityByHour;

	public UserData(String id)
	{
		m_id = id;
		m_activityByHour = new int[24];
	}

	public String getID(){return m_id;}
	public int getCreated(){return m_created;}
	public int getModified(){return m_modified;}
	private int getActivityCounter(){
		int counter = 0;
		for (int i=0; i<m_activityByHour.length; i++){
			counter += m_activityByHour[i];
		}
		return counter;
	}

	public void blipCreated()
	{
		m_created++;
	}

	public void blipModified()
	{
		m_modified++;
	}
	
	public boolean getIsParticipant(){
		return m_isParticipant;
	}


	public void setInviter(String inviter)
		{
			if(m_inviter != null)
			{
				log.warning("Trying to set "+inviter+" as inviter of "+m_id+" but I already have an inviter ... "+m_inviter);
			}
			m_inviter = inviter;
		}

	@Override
		public String toString()
		{
			// TODO Auto-generated method stub
			return "user=" + m_id + ", created=" + m_created + ", modified=" + m_modified+ " (invited by "+m_inviter+")";
		}

	public boolean isActive(){
		return ((m_created > 0) || (m_modified > 0));
	}

	public boolean moreActive(UserData otherUser) {
		// TODO Auto-generated method stub
		if (otherUser == null){
			return true;
		}
		else{
			return getActivityCounter() > otherUser.getActivityCounter();
		}

	}

	public void blipSubmitted(long timestamp) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(timestamp);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		m_activityByHour[hour]++;
	}

	public int[] getActivityByHours() {

		return m_activityByHour;
	}

	public void setIsParticipant(boolean b) {
		m_isParticipant = b;
	}

	public int getWaveActivityBetween(int startHour, int endHour) {
		int retVal = 0;
		for(int i=startHour; i<=endHour;i++){
			retVal += m_activityByHour[i];
		}
		//log.warning("User's ("+m_id+") activity between "+startHour+ " and "+endHour+" is "+retVal);
		return retVal;
	}

}
